package com.song.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.song.mapper.StudentMapper;
import com.song.pojo.PageResult;
import com.song.pojo.Student;
import com.song.pojo.StudentQueryParam;
import com.song.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceIml implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public PageResult<Student> getStudents(StudentQueryParam param) {
        PageHelper.startPage(param.getPage(),param.getPageSize());
      List<Student> students = studentMapper.getStudents(param);
      Page<Student> p = (Page<Student>) students;
      return new PageResult<Student>(p.getTotal(), p.getResult());
    }

    @Override
    public Integer addStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        return studentMapper.addStudent(student);
    }

    @Override
    public Integer deleteStudent(List<Integer> ids) {
      return studentMapper.deleteStudentByIds(ids);
    }

    @Override
    public Student getStudent(Integer id) {
      return studentMapper.getStudentById(id);
    }

    @Override
    public Integer updateStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        return studentMapper.updateStudent(student);
    }

    @Override
    public Integer handleStudentViolation(Integer id, Short score) {
        return studentMapper.handleStudentViolation(id, score);
    }
}
